<template>
	<div style="padding:0px;">
		<el-card class="box-card">
		  <div  class="text item" >
			  <el-form :model="dialogForm" ref="dialogForm" :label-width="formLabelWidth" :rules="rules">
			    <!-- <el-form-item label="汽车品牌名称"  prop="carBrand.name">			      
			    				   <el-select v-model="dialogForm.qcsl" filterable placeholder="请选择汽车品牌" disabled>
			    				    <el-input v-model="dialogForm.qcsl"  auto-complete="off" :disabled="!dialogSendData.isAdd"></el-input>
			    				   </el-select>
			    </el-form-item> -->
			    <brandSelect v-if="dialogForm.carBrandId||dialogForm.carBrandId==null"  :pinpaiId="dialogForm.carBrandId"  :isDisabled="!dialogSendData.isAdd||dialogSendData.id!==''" v-on:receive-brand-data="receiveBrandData" :formLabelWidth="formLabelWidth" level=0></brandSelect>
			    <el-form-item label="汽车子品牌名称"  prop="name">
			        <el-input v-model="dialogForm.name" :maxlength="20" auto-complete="off" :disabled="!dialogSendData.isAdd"></el-input>
			    </el-form-item>
			    <el-form-item label="货源渠道"  prop="sourceType">			      
					<el-select v-model="dialogForm.sourceType" filterable placeholder="请选择货源渠道" :disabled="!dialogSendData.isAdd">
				    	<el-option label="平行进口" :value="1" ></el-option>
				    	<el-option label="中规进口" :value="2" ></el-option>
				    	<el-option label="非进口" :value="3" ></el-option>
				   	</el-select>
			    </el-form-item>
			    <el-form-item label="备注" >
			      <el-input v-model="dialogForm.remark" type="textarea" :rows=3 auto-complete="off" :disabled="!dialogSendData.isAdd"></el-input>
			    </el-form-item>		   			    
			  </el-form>
			</div>
		</el-card>
		<div  style="text-align:right;margin-top:15px;">
			 <el-button @click="returnBack">返 回</el-button>
			 <el-button v-if="dialogSendData.isAdd" type="primary" @click="submitConfirmForm('dialogForm')">提 交</el-button>
		</div>
	</div>
</template>
<script>
    import agentHttp from '@/utils/agentHttp.js'
    import brandSelect from '@/views/brandSelect/base.vue'
	export default{
		created(){		
			if(this.dialogSendData.id!==""){
	      		this.selectData(); 
	      		//console.info(this.dialogSendData.isAdd);
	      	}else{
	      		this.dialogForm.carBrandId=null
	      	}
        },        
        components:{brandSelect},
		props:['dialogSendData','tableData'],
		data(){
			return{				
				formLabelWidth:'170px',
				//弹窗或者查看页面信息
		        dialogForm:{	
		           carBrandId:"",
		           sourceType:""	         
		        },
		        //验证规则
		        rules: {	
		            name: [
		              { required: true, message: '汽车子品牌不能为空', trigger: 'blur' }
		            ],
		            sourceType:[
		              { required: true, message: '货源渠道不能为空', trigger: 'change' ,type:'number'}
		            ],	          
		        }
			}
		},methods:{
			receiveBrandData(pinpai,zipinpai,chexi,chexing,data){ //依次为 品牌id： 子品牌id:  车系id:  车型id:
				this.$nextTick(()=>{
					this.dialogForm.carBrandId=pinpai;
				})
			},
			returnBack(){
				this.dialogSendData.dialogFormVisible=false;
			},
			submitConfirmForm(formName){
				if(this.dialogForm.carBrandId==""||this.dialogForm.carBrandId==null){
		    		this.$message.warning("请选择汽车品牌");
		            return false;
		    	}
                this.$refs[formName].validate((valid) => {
		          if (valid) {
		          	if(this.dialogSendData.id===""){
		          	    this.addData();
		          	}else{
		          		this.editData();
		          	}		          	
		          } else {
		             this.$nextTick(()=>{
		          		this.$message.warning(document.getElementsByClassName('el-form-item__error')[0].innerText);
		                return false;
		          	})
		          }
		        });
			},
		    addData(){
		    	
		        agentHttp.postJson('adminApi/admin/car_sub_brand/add',this.dialogForm).then((data)=>{
		            if(data.success){  
		            	this.$emit('refreshTableData', null);
		            	this.$message.success(data.data||"添加成功");
		            	setTimeout(()=>{
		            		this.dialogSendData.dialogFormVisible=false;
		            	},500);
		            } 
		        }) 
		    },
		    editData(){
		    	
		        agentHttp.putJson('adminApi/admin/car_sub_brand/update',this.dialogForm).then((data)=>{
		            if(data.success){  
		            	this.$emit('refreshTableData', null);
		            	this.$message.success(data.data||"修改成功");
		            	setTimeout(()=>{
		            		this.dialogSendData.dialogFormVisible=false;
		            	},500);
		            } 
		        }) 
		    },
		    selectData(){
		        agentHttp.get('adminApi/admin/car_sub_brand/view/'+this.dialogSendData.id).then((data)=>{	
		            if(data.success){  
		            	this.dialogForm=data.data;
		            } 
		        }) 
		    },
		}
	}
</script>
